package test.dad;

import com.vaadin.event.dd.DragAndDropEvent;
import com.vaadin.event.dd.DropHandler;
import com.vaadin.event.dd.acceptcriteria.AcceptAll;
import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.Button;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.DragAndDropWrapper;
import com.vaadin.ui.AbsoluteLayout.ComponentPosition;
import com.vaadin.ui.DragAndDropWrapper.DragStartMode;
import com.vaadin.ui.DragAndDropWrapper.WrapperTargetDetails;
import com.vaadin.ui.DragAndDropWrapper.WrapperTransferable;

public class DAD extends CustomComponent {
	private static final long serialVersionUID = -8301626324227210030L;
    public void attach(){
    	warstwa();
    }
    void warstwa(){
    	final AbsoluteLayout absLayout=new AbsoluteLayout();
    	absLayout.setWidth("600px");
        absLayout.setHeight("300px");
        
        Button mojprzycisk=new Button("Przycisk 1");
        DragAndDropWrapper wrapper=new DragAndDropWrapper(mojprzycisk);
        wrapper.setDragStartMode(DragStartMode.WRAPPER);
        absLayout.addComponent(wrapper,"left: 50px; top: 50px;");
        DragAndDropWrapper abswrapper=new DragAndDropWrapper(absLayout);
        class MojUchwyt implements DropHandler{

			public void drop(DragAndDropEvent event) {
				/*
            	 * pobieranie wrapper-a danego obiektu
            	 */
                WrapperTransferable t =
                    (WrapperTransferable) event.getTransferable();
                WrapperTargetDetails details =
                        (WrapperTargetDetails) event.getTargetDetails();
                int x=details.getMouseEvent().getClientX()
                        - t.getMouseDownEvent().getClientX();
                int y=details.getMouseEvent().getClientY()
                        - t.getMouseDownEvent().getClientY();
                ComponentPosition pos =
            			absLayout.getPosition(t.getSourceComponent());
            	pos.setLeftValue(pos.getLeftValue() + x);
            	pos.setTopValue(pos.getTopValue() + y);
			}

			public AcceptCriterion getAcceptCriterion() {
				return AcceptAll.get();
			}
        }
        abswrapper.setDropHandler(new MojUchwyt());
        setCompositionRoot(abswrapper);
    }
}
